<template>
  <div>
    <el-tree
      :data="menus"
      show-checkbox
      :props="defaultProps"
      :expand-on-click-node="false"
      node-key="catId"
      :default-expanded-keys="defaultkeys"
      draggable
      :allow-drop="allowDrop"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="node.level <= 2"
            type="text"
            size="mini"
            @click="append(data)"
            >添加</el-button
          >
          <el-button type="text" size="mini" @click="edit(data)"
            >修改</el-button
          >
          <el-button
            v-if="node.childNodes.length == 0"
            type="text"
            size="mini"
            @click="() => remove(node, data)"
            >删除</el-button
          >
        </span>
      </span>
    </el-tree>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="30%">
      <!-- sds -->
      <el-form ref="form" :model="category" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="category.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-input v-model="category.icon" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="计量单位">
          <el-input
            v-model="category.productUnit"
            auto-complete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <!--  -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitData()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      maxlevel: 0,
      title: "",
      type: "",
      category: {
        name: "",
        parentCid: 0,
        catLevel: 0,
        showStatus: 1,
        sort: 0,
        productUnit: "",
        icon: "",
        catId: null,
      },
      dialogVisible: false,
      defaultkeys: [],
      menus: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  methods: {
    getMenus() {
      this.$http({
        url: this.$http.adornUrl("/product/category/list/tree"),
        method: "get",
      }).then(({ data }) => {
        console.log("成功获取到菜单数据...", data.data);
        this.menus = data.data;
      });
    },
    handleNodeClick(data) {
      console.log(data);
    },
    append(data) {
      this.type = "apped";
      this.title = "添加";
      this.dialogVisible = true;
      this.category.parentCid = data.catId;
      this.category.catLevel = data.catLevel * 1 + 1;
      this.category.name = "";
      this.category.icon = "";
      this.category.productUnit = "";
      this.catId = null;
      this.sort = 0;
      this.showStatus = 1;
    },
    remove(node, data) {
      var ids = [data.catId];
      this.$confirm(`此操作将永久删除${data.name}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl(`/product/category/delete`),
            method: "post",
            data: this.$http.adornData(ids, false),
          }).then(({ data }) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getMenus();
            this.defaultkeys = [node.parent.data.catId];
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
      console.log("remove", node, data);
    },
    addcategory() {
      console.log("append", this.category);
      this.$http({
        url: this.$http.adornUrl("/product/category/save"),
        method: "post",
        data: this.$http.adornData(this.category, false),
      }).then(({ data }) => {
        this.$message({
          type: "success",
          message: "保存成功!",
        });
        this.dialogVisible = false;
        this.getMenus();
        this.defaultkeys = [this.category.parentCid];
      });
    },
    submitData() {
      if (this.type == "apped") {
        this.addcategory();
      }
      if (this.type == "edit") {
        this.editcategory();
      }
    },
    edit(data) {
      this.type = "edit";
      this.dialogVisible = true;
      this.title = "修改";
      this.category.name = data.name;
      this.$http({
        url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
        method: "get",
      }).then(({ data }) => {
        this.category.name = data.data.name;
        this.category.catId = data.data.catId;
        this.category.icon = data.data.icon;
        this.category.productUnit = data.data.productUnit;
        this.category.parentCid = data.data.parentCid;
        this.category.catLevel = data.data.catLevel;
        this.category.sort = data.data.sort;
        this.category.showStatus = data.data.showStatus;
      });
    },
    editcategory() {
      var { catId, name, icon, productUnit } = this.category;
      this.$http({
        url: this.$http.adornUrl("/product/category/update"),
        method: "post",
        data: this.$http.adornData({ catId, name, icon, productUnit }, false),
      }).then(({ data }) => {
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        this.dialogVisible = false;
        this.getMenus();
        this.defaultkeys = [this.category.parentCid];
      });
    },
    //判断是否可以拖动
    allowDrop(draggingNode, dropNode, type) {
      this.countNodeLevel(draggingNode.data);
      let deep=this.maxlevel-draggingNode.data.catLevel+1;
      if(type=="inner"){
          return (deep+dropNode.level)<=3
      }
      else{
          return (deep+dropNode.parent.level)<=3
      }
    },
    //求最大深度
    countNodeLevel(node) {
        if(node.children!=null&&node.children.length>0){
            for(let i=0;i<node.children.length;i++){
                if(node.children[i].catLevel>this.maxlevel){
                    this.maxlevel=node.children[i].catLevel
                }
                this.countNodeLevel(node.children[i])
            }
        }
    },
  },
  created() {
    this.getMenus();
  },
};
</script>

<style>
</style>